﻿@model IEnumerable<OPM.Models.ChannelIn>
@using OPM.Class;
@using OPM.Class;
@using System.Web.Mvc;

@{
    Layout = "~/Views/Shared/_LayoutPopup.cshtml";

    // recollect from get, incase re popup
    string cont_is_secret = Request["cont_is_secret"]??"";
    string cont_tel_all = Request["cont_tel_all"]??"";
    string cont_country_id = Request["cont_country_id"]??"";
    string cont_type = Request["cont_type"]??"";
    string cont_gender_id = Request["cont_gender_id"]??"";
    string cont_mobile_all = Request["cont_mobile_all"]??"";
    string cont_title = Request["cont_title"]??"";
    string cont_firstname = Request["cont_firstname"]??""; 
    string cont_lastname = Request["cont_lastname"]??"";
    string cont_email_send_all = Request["cont_email_send_all"]??"";
    string cont_citizen_id = Request["cont_citizen_id"]??"";

    string cont_address = Request["cont_address"]??"";
    string cont_province_id = Request["cont_province_id"]??"";
    string cont_district_id = Request["cont_district_id"]??"";
    string cont_subdistrict_id = Request["cont_subdistrict_id"]??"";
    string cont_is_defective = Request["cont_is_defective"]??"";
    string cont_postcode = Request["cont_postcode"]??"";
    string cont_number = Request["cont_number"]??"";
    string cont_remark = Request["cont_remark"]??"";
    string cont_birthday = Request["cont_birthday"]??"";


    string province_text = Request["province_text"]??"";
    string district_text = Request["district_text"]??"";
    string subdistrict_text = Request["subdistrict_text"]??"";

}

<div class="service_search_header">
    <div class="service_title">@Multi.contact1</div>
</div>
<div class="service_box_table" style="padding: 0px 0px 0px 20px; overflow: initial;">
    <table id="service_table">
        <tr>
            <td>@Multi.is_secret</td>
            <td>
                <select id="cont_is_secret">
                    <option value="" @(cont_is_secret == "")>เลือก</option>
                    <option value="F" @(cont_is_secret == "F")>เปิดเผย</option>
                    <option value="T" @(cont_is_secret == "T")>ปกปิด</option>
                </select>
            </td>
            <td>โทรศัพท์</td>
            <td>
                <input type="tel" name="cont_tel" value="" id="cont_tel" />
                <div class="pin_add" onclick="addstrtotext('cont_tel','cont_tel_all');"></div>
            </td>
        </tr>
        <tr>
            <td>@Multi.cont_type</td>
            <td>
                <select id="cont_type">
                    <option value="" @(string.IsNullOrWhiteSpace(cont_type)?"selected":"")>เลือก</option>
                    <option value="C" @("C" == (cont_type)?"selected":"")>บุคคล</option>
                    <option value="O" @("O" == (cont_type)?"selected":"")>หน่วยงาน/องค์กร</option>
                    <option value="M" @("M" == (cont_type)?"selected":"")>กลุ่มบุคคล</option>
                </select>
            </td>
            <td rowspan="2">&nbsp;</td>
            <td rowspan="2" class="tdBottom">
                <textarea style="height: 48px;" id="cont_tel_all">@cont_tel_all</textarea>
                <div class="pin_del" style="top: -25px;" onclick="deltext('cont_tel_all');"></div>
            </td>
        </tr>
        <tr>
            <td>@Multi.country</td>
            <td>
                <select id="cont_country_id" name="cont_country_id"> 
                    @foreach (var x in (SelectList)ViewBag.listOfCountry) { 
                        <option value="@x.Value" @(x.Value == cont_country_id || 
                        (string.IsNullOrWhiteSpace(cont_country_id) && x.Text == "ไทย") ?"selected":"")>@x.Text</option> 
                    }
                </select>
            </td>
        </tr>
        <tr>
            <td>@Multi.gender</td>
            <td>
                <select id="cont_gender_id" name="cont_gender_id">
                    @foreach (var x in (SelectList)ViewBag.listOfGender) {
                        <option value="@x.Value" @(x.Value == cont_gender_id ?"selected":"")>@x.Text</option>
                    }
                </select>
            </td>
            <td>@Multi.mobile</td>
            <td>
                <input type="tel" name="cont_mobile" id="cont_mobile" value="" />
                <div class="pin_add" onclick="addstrtotext('cont_mobile','cont_mobile_all');"></div>
            </td>
        </tr>
        <tr>
            <td class="grouphide2">@Multi.title_name</td>
            <td class="grouphide2">
                <input type="text" name="cont_title" id="cont_title" value="@cont_title"></td>
            <td rowspan="2">&nbsp;</td>
            <td rowspan="2" class="tdBottom">
                <textarea style="height: 48px;" id="cont_mobile_all">@cont_mobile_all</textarea>
                <div class="pin_del" style="top: -25px;" onclick="deltext('cont_mobile_all');"></div>
            </td>
        </tr>
        <tr>
            <td>@Multi.name</td>
            <td>
                <input type="text" name="cont_firstname" id="cont_firstname" value="@cont_firstname"></td>
        </tr>
        <tr>
            <td class="grouphide2">@Multi.lastname</td>
            <td class="grouphide2">
                <input type="text" name="cont_lastname" id="cont_lastname" value="@cont_lastname"></td>
            <td>@Multi.email</td>
            <td>
                <input type="text" name="cont_email_send" value="" id="cont_email_send" />
                <div class="pin_add" onclick="addstrtotext('cont_email_send','cont_email_send_all');"></div>
            </td>
        </tr>
        <tr>
            <td>@Multi.citizen_id</td>
            <td>
                <input type="text" name="cont_citizen_id" id="cont_citizen_id" value="@cont_citizen_id"></td>
            <td rowspan="2">&nbsp;</td>
            <td rowspan="2" class="tdBottom">
                <textarea style="height: 48px;" id="cont_email_send_all">@cont_email_send_all</textarea>
                <div class="pin_del" style="top: -25px;" onclick="deltext('cont_email_send_all');"></div>
            </td>
        </tr>
        <tr>
            <td>@Multi.address</td>
            <td>
                <input type="text" name="cont_address" id="cont_address" value="@cont_address"></td>
        </tr>
        <tr>
            <td></td>
            <td><span class="note">@Multi.area</span></td>
        </tr>
        <tr>
            <td>@Multi.case_area</td>
            <td>
                <a id="district_achor" onclick="parent.callcolorboxrecursive(getareaurl(), sendDataToParent(true), false); return false;"
                   data-block="block_crime" class="formBtnArea dBlock">@Multi.local_scene</a>
                <p style="color: gray" id="locationToRedirect">@province_text @district_text @subdistrict_text</p>
                <input type="hidden" name="country_id" id="country_id" value="@cont_country_id" />
                <input type="hidden" name="province_id" id="province_id" value="@cont_province_id" />
                <input type="hidden" name="district_id" id="district_id" value="@cont_district_id" />
                <input type="hidden" name="subdistrict_id" id="subdistrict_id" value="@cont_subdistrict_id" />
            </td>
        </tr>
        <tr>
            <td class="grouphide1">@Multi.cont_number</td>
            <td class="grouphide1">
                <input type="text" name="cont_number" id="cont_number" class="half-input" value="0">
                @Multi.people</td>
        </tr>
        <tr>
            @*<td></td>
            <td><a class="formBtn dBlock" onclick="popup('@Url.Action("GetCrime", "Complaint")','location',300,300);" href="#">จังหวัด/อำเภอ/ตำบล</a></td>*@
            <td class="grouphide2">@Multi.type</td>
            <td class="grouphide2">
                <select id="cont_is_defective" name="cont_is_defective" class="">
                    <option value="">เลือก</option>
                    <option value="T">ปกติ</option>
                    <option value="F">ผู้พิการ</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>@Multi.postcode</td>
            <td>
                <input type="text" name="cont_postcode" id="cont_postcode" value=""></td>
            <td rowspan="2">หมายเหตุ</td>
            <td rowspan="2" class="tdBottom">
                <textarea style="height: 48px;" id="cont_remark"></textarea>
            </td>
        </tr>
        <tr>
            <td class="grouphide2">@Multi.birthdate</td>
            <td class="grouphide2">
                <input type="date" name="cont_birthday" class="cont_birthday" id="cont_birthday" value="" />
            </td>

        </tr>
    </table>
</div>
<div class="BoxServiceBottom">
    <div class="BoxServiceActionPrev floatL" onclick="parent.$.fn.colorbox.close();">@Multi.Cancel</div>
    <div class="BoxServiceActionNext floatR" onclick="sendDataToParent();">@Multi.add</div>
    <div class="clear"></div>
</div>
<script src="~/js/jquery-ui.min.js"></script>

<script type="text/javascript">

    var locationToRedirect = '';

    $(document).ready(function () {

        if (parent && parent.formJson && parent.formJson.locationToRedirect) {
            $('#locationToRedirect').html(parent.formJson.locationToRedirect);
        }




        $("#cont_type").change(function () {
            var myType = $(this).val();
            if (myType == 'C') {
                $(".grouphide1 input , .grouphide1 select").prop('disabled', true);
                $(".grouphide2 input , .grouphide2 select").prop('disabled', false);
                $(".grouphide1").css('color', '#aaa');
                $(".grouphide2").css('color', '#000');
                $(".grouphide1 img").css('opacity', '0');
                $(".grouphide2 img").css('opacity', '1');
            } else if (myType == 'O') {
                $(".grouphide1 input , .grouphide1 select").prop('disabled', false);
                $(".grouphide2 input , .grouphide2 select").prop('disabled', true);
                $(".grouphide1").css('color', '#000');
                $(".grouphide2").css('color', '#aaa');
                $(".grouphide1 img").css('opacity', '1');
                $(".grouphide2 img").css('opacity', '0');
            } else if (myType == 'M') {
                $(".grouphide1 input , .grouphide1 select").prop('disabled', false);
                $(".grouphide2 input , .grouphide2 select").prop('disabled', true);
                $(".grouphide1").css('color', '#000');
                $(".grouphide2").css('color', '#aaa');
                $(".grouphide1 img").css('opacity', '1');
                $(".grouphide2 img").css('opacity', '0');
            } else {
                $(".grouphide1 input , .grouphide1 select").prop('disabled', false);
                $(".grouphide2 input , .grouphide2 select").prop('disabled', false);
                $(".grouphide1").css('color', '#000');
                $(".grouphide2").css('color', '#000');
                $(".grouphide1 img").css('opacity', '1');
                $(".grouphide2 img").css('opacity', '1');
            }
        });

        $("#cont_country_id").change(function () {
            var Country = $("#cont_country_id option:selected").text();

            if (Country == 'ไทย') {
                $("#tabProvince , #tabDistricts , #tabSubDistricts").show();
            } else {
                $("#tabProvince , #tabDistricts , #tabSubDistricts").hide();
            }
        });
        $("#cboProvince").change(function () {
            var url = "@Url.Content("~/Complaint/GetDistrictsJson")?provinceId=" + $(this).val();
            $.getJSON(url)
              .done(function (json) {
                  $('#cboDistricts').html('');
                  $.each(json, function (i, data) {
                      $('#cboDistricts').append(
                          '<option value="' + data.Id + '">' + data.Name + '</option>'
                      );
                  }
              )
              })
              .fail(function (jqxhr, textStatus, error) {
                  var err = textStatus + ", " + error;
                  console.log("Request Failed: " + err);
              });
        });

        $("#cboDistricts").change(function () {
            var url = "@Url.Content("~/Complaint/GetSubDistrictsJson")?districtId=" + $(this).val();
                $.getJSON(url)
                  .done(function (json) {
                      $('#cboSubDistricts').html('');
                      $.each(json, function (i, data) {
                          $('#cboSubDistricts').append(
                              '<option value="' + data.Id + '">' + data.Name + '</option>'
                          );
                      }
                  )
                  })
                  .fail(function (jqxhr, textStatus, error) {
                      var err = textStatus + ", " + error;
                      console.log("Request Failed: " + err);
                  });
            });
        $("#cboSubDistricts").change(function () {
            locationToRedirect += $("#cboSubDistricts option:selected").text() + " ";
            locationToRedirect += $("#cboDistricts option:selected").text() + " ";
            locationToRedirect += $("#cboProvince option:selected").text() + " ";
            locationToRedirect += $("#cont_country_id option:selected").text();
            parent.addTextLocation2(locationToRedirect);
            console.log(locationToRedirect);
    
        });

        if (parent && parent.formJson) {
            $('#cont_is_secret').val(parent.formJson.cont_is_secret);
            $('#cont_tel_all').val(parent.formJson.cont_tel_all);
            $('#cont_country_id').val(parent.formJson.cont_country_id);
            $('#cont_type').val(parent.formJson.cont_type);
            $('#cont_gender_id').val(parent.formJson.cont_gender_id);
            $('#cont_mobile_all').val(parent.formJson.cont_mobile_all);
            $('#cont_title').val(parent.formJson.cont_title);
            $('#cont_firstname').val(parent.formJson.cont_firstname);
            $('#cont_lastname').val(parent.formJson.cont_lastname);
            $('#cont_email_send_all').val(parent.formJson.cont_email_send_all);
            $('#cont_citizen_id').val(parent.formJson.cont_citizen_id);
            $('#cont_address').val(parent.formJson.cont_address);
            $('#locationToRedirect').html(parent.formJson.locationToRedirect);
            $('#cont_province_id').val(parent.formJson.cont_province_id);
            $('#cont_district_id').val(parent.formJson.cont_district_id);
            $('#cont_subdistrict_id').val(parent.formJson.cont_subdistrict_id);
            $('#cont_is_defective').val(parent.formJson.cont_is_defective);
            $('#cont_postcode').val(parent.formJson.cont_postcode);
            $('#cont_number').val(parent.formJson.cont_number);
            $('#cont_remark').val(parent.formJson.cont_remark);
            $('#cont_birthday').val(parent.formJson.cont_birthday);
            $('#cont_contact_detail').val(parent.formJson.cont_contact_detail);
        }


    });

    function setToParentJson(name, value) {
        if (!parent.$("#"+name)) {
            parent.append('<input type="hidden" name="' + name + '" id="' + name + '" value="' + value + '" />');

        } else {
            parent.$("#" + name).val(value);
        }
    }
    function sendDataToParent(isJustCopyJson) {
        if (parent && !parent.formJson) {
                parent.formJson = new Object();
        }
        var cont_is_secret = $("#cont_is_secret").val();
        //        parent.$("#cont_is_secret").val(cont_is_secret); //ปกปิดข้อมูล
        setToParentJson('cont_is_secret', cont_is_secret);
        parent.formJson.cont_is_secret = cont_is_secret;

        var cont_tel_all = $("#cont_tel_all").val();
        //parent.$("#cont_tel_all").val(cont_tel_all); //โทรศัพท์
        setToParentJson('cont_tel_all', cont_tel_all);
        parent.formJson.cont_tel_all = cont_tel_all;

        var cont_country_id = $("#cont_country_id").val();
        //parent.$("#cont_country_id").val(cont_country_id); //ประเทศ
        setToParentJson('cont_country_id', cont_country_id);
        parent.formJson.cont_country_id = cont_country_id;

        var cont_type = $("#cont_type").val();//ประเภทบุคคล
        //parent.$("#cont_type").val(cont_type);
        setToParentJson('cont_type', cont_type);
        parent.formJson.cont_type = cont_type;

        var cont_gender_id = $("#cont_gender_id").val();
        //parent.$("#cont_gender_id").val(cont_gender_id); //เพศ
        setToParentJson('cont_gender_id', cont_gender_id);
        parent.formJson.cont_gender_id = cont_gender_id;

        var cont_mobile_all = $("#cont_mobile_all").val();
        //parent.$("#cont_mobile_all").val(cont_mobile_all); //โทรศัพท์เคลื่อนที่
        setToParentJson('cont_mobile_all', cont_mobile_all);
        parent.formJson.cont_mobile_all = cont_mobile_all;

        var cont_title = $("#cont_title").val();
        //parent.$("#cont_title").val(cont_title);
        setToParentJson('cont_title', cont_title);
        parent.formJson.cont_title = cont_title;


        var cont_firstname = $("#cont_firstname").val();
        //parent.$("#cont_firstname").val(cont_firstname);
        setToParentJson('cont_firstname', cont_firstname);
        parent.formJson.cont_firstname = cont_firstname;
         
        var cont_lastname = $("#cont_lastname").val();
        //parent.$("#cont_lastname").val(cont_lastname); //นามสกุล
        setToParentJson('cont_lastname', cont_lastname);
        parent.formJson.cont_lastname = cont_lastname;

        var cont_email_send_all = $("#cont_email_send_all").val();
        //parent.$("#cont_email_send_all").val(cont_email_send_all); //อีเมล์
        setToParentJson('cont_email_send_all', cont_email_send_all);
        parent.formJson.cont_email_send_all = cont_email_send_all;

        var cont_citizen_id = $("#cont_citizen_id").val();
        //parent.$("#cont_citizen_id").val(cont_citizen_id); //บัตรประชาชน
        setToParentJson('cont_citizen_id', cont_citizen_id);
        parent.formJson.cont_citizen_id = cont_citizen_id;

        var cont_address = $("#cont_address").val();
        //parent.$("#cont_address").val(cont_address); //ที่อยู่
        setToParentJson('cont_address', cont_address);
        parent.formJson.cont_address = cont_address;

        var cont_province_id = $("#cboProvince").val();
        //parent.$("#cont_province_id").val(cont_province_id); //จังหวัด
        setToParentJson('cont_province_id', cont_province_id);
        parent.formJson.cont_province_id = cont_province_id;

        var cont_district_id = $("#cboDistricts").val();
        //parent.$("#cont_district_id").val(cont_district_id); //อำเภอ
        setToParentJson('cont_district_id', cont_district_id);
        parent.formJson.cont_district_id = cont_district_id;

        var cont_subdistrict_id = $("#cboSubDistricts").val();
        //parent.$("#cont_subdistrict_id").val(cont_subdistrict_id); //ตำบล
        setToParentJson('cont_subdistrict_id', cont_subdistrict_id);
        parent.formJson.cont_subdistrict_id = cont_subdistrict_id;

        var cont_is_defective = $("#cont_is_defective").val();
        //parent.$("#cont_is_defective").val(cont_is_defective); //ประเภท
        setToParentJson('cont_is_defective', cont_is_defective);
        parent.formJson.cont_is_defective = cont_is_defective;

        var cont_postcode = $("#cont_postcode").val();
        //parent.$("#cont_postcode").val(cont_postcode); //รหัสไปรษณีย์
        setToParentJson('cont_postcode', cont_postcode);
        parent.formJson.cont_postcode = cont_postcode;

        var cont_number = $("#cont_number").val();
        //parent.$("#cont_number").val(cont_number); //จำนวน
        setToParentJson('cont_number', cont_number);
        parent.formJson.cont_number = cont_number;

        var cont_remark = $("#cont_remark").val();
       // parent.$("#cont_remark").val(cont_remark); //หมายเหตุ
        setToParentJson('cont_remark', cont_remark);
        parent.formJson.cont_remark = cont_remark;

        var cont_birthday = $("#cont_birthday").val();
        //parent.$("#cont_birthday").val(cont_birthday); //วันเกิด
        setToParentJson('cont_birthday', cont_birthday);
        parent.formJson.cont_birthday = cont_birthday;

        var cont_contact_detail= "";
        if (cont_tel_all == "") {

            cont_contact_detail = cont_tel_all;

        } else if (cont_mobile_all == "") {
            cont_contact_detail = cont_tel_all +","+ cont_mobile_all;

        }
        else if(cont_email_send_all == ""){
            cont_contact_detail = cont_tel_all +","+ cont_mobile_all+","+cont_email_send_all;
        }
        else {
            cont_contact_detail = cont_contact_detail;
        }

        var cont_contact_detail = $("#cont_contact_detail").val();
        //parent.$("#cont_contact_detail").val(cont_contact_detail); //
        setToParentJson('cont_contact_detail', cont_contact_detail);
        parent.formJson.cont_contact_detail = cont_contact_detail;

        if (isJustCopyJson != true && cont_contact_detail == "") {
            alert("โปรดระบุหมายเลขโทรศัพท์หรืออีเมล์");
        }
        else if (isJustCopyJson != true)
        {
            //alert('close'              );
            parent.$.fn.colorbox.close();
        }
    }

    function addstrtotext(boxinput, boxoutput) {
        var str  = $('#' + boxinput).val();
        var str2 = $('#' + boxoutput).val();
        if (str2 != '') {
            str = str2 + ',' + str;
        } else {
            str = str
        }
        $('#' + boxoutput).val(str);
        $('#' + boxinput).val('').focus();
    }
    function deltext(boxoutput) {
        $('#' + boxoutput).val('');
    }

    function emptyIfUndefined(v) {
        if (v == undefined) {
            return '';
        }
        return v;
    }
    function getareaurl() { 
        var link = '/Complaint/GetCrime?a=crime&typereturn=alternate&country_id=' + emptyIfUndefined(parent.formJson.cont_country_id) +
            '&province_id=' + emptyIfUndefined(parent.formJson.cont_province_id) +
            '&district_id=' + emptyIfUndefined(parent.formJson.cont_district_id) +
            '&subdistrict_id=' + emptyIfUndefined(parent.formJson.cont_subdistrict_id);
        // alert('getareaurl' + link);
        return link;
    }
</script>
